package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.entity.SysMenu;
import com.huike.entity.SysRoleMenu;
import com.huike.exception.BaseException;
import com.huike.mapper.MenuMapper;
import com.huike.dto.MenuPageDto;
import com.huike.mapper.SysRoleMenuMapper;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 菜单分页模糊查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result<PageResult> list(MenuPageDto dto) {

        // 1.校验数据
        if (dto == null) {
            return Result.error("数据错误");
        }
        LambdaQueryWrapper<SysMenu> lqw = new LambdaQueryWrapper<>();
        if (dto.getName() != null && !dto.getName().equals("")) {
            lqw.like(SysMenu::getMenuName, dto.getName());
        }
        if (dto.getStatus() != null) {
            lqw.eq(SysMenu::getStatus, dto.getStatus());
        }

        lqw.orderByAsc(SysMenu::getOrderNum);
        // 2.分页查询
        IPage page = new Page(dto.getPageNum(), dto.getPageSize());
        page = menuMapper.selectPage(page, lqw);

/*        // 3.获取总数据
        List<SysMenu> menuList = page.getRecords();

        List<MenuVo> menuVoList = new ArrayList<>();
        for (SysMenu menu : menuList) {
            if (menu.getParentId() == 0) {
                MenuVo menuVo = new MenuVo();
                BeanUtils.copyProperties(menu, menuVo);
                menuVoList.add(menuVo);
            }
        }

        for (MenuVo menuVo : menuVoList) {
            List<SysMenu> menus = new ArrayList<>();
            for (SysMenu menu : menuList) {
                if (menuVo.getMenuId().equals(menu.getParentId())) {
                    menus.add(menu);
                }
            }
            menuVo.setSysMenus(menus);
        }*/

        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());

        return Result.success(pageResult);
    }

    /**
     * 菜单目录回显
     *
     * @return
     */
    @Override
    public Result all() {
        List<SysMenu> sysMenus = menuMapper.allMenuName();
        return Result.success(sysMenus);
    }

    /**
     * 删除
     *
     * @param menuId
     * @return
     */
    @Override
    public Result deleteById(Long menuId) {

        // 是否是父菜单被关联
        LambdaQueryWrapper<SysMenu> lqwMens = new LambdaQueryWrapper<>();
        List<SysMenu> sysMenus = menuMapper.selectList(lqwMens.eq(SysMenu::getParentId, menuId));
        if (sysMenus != null && sysMenus.size() > 0) {
            throw new BaseException("含有子菜单无法关联");
        }

        // 1.查询是否被关联
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(lqw.eq(SysRoleMenu::getMenuId, menuId));
        if (sysRoleMenus != null && sysRoleMenus.size() > 0) {
            throw new BaseException("菜单被关联无法删除");
        }
        // 2.没有被关联可以删除
        menuMapper.deleteById(menuId);
        return Result.success();
    }

    /**
     * 保存菜单
     *
     * @param dto
     * @return
     */
    @Override
    public Result saveMenu(SysMenu dto) {
        dto.setCreateTime(LocalDateTime.now());
        //TODO 创建人
        dto.setCreateBy("admin");
        menuMapper.insert(dto);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Result updateById(SysMenu dto) {

        menuMapper.updateById(dto);

        return Result.success();
    }
}
